package com.tianyang.yukuaifu.business.main.fragment;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseFragment;
import com.tianyang.yukuaifu.business.auth.provider.AuthOpenProvider;
import com.tianyang.yukuaifu.business.login.provider.LoginOpenProvider;
import com.tianyang.yukuaifu.business.main.bean.UserCenterOutput;
import com.tianyang.yukuaifu.business.main.provider.MainOpenProvider;
import com.tianyang.yukuaifu.business.trade.provider.TradeOpenProvider;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.module.dialog.HintDialog;
import com.tianyang.yukuaifu.module.dialog.RemindDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.OnBtnClickL;
import com.tianyang.yukuaifu.module.dialog.interfaces.TwoBtnClickListener;
import com.tianyang.yukuaifu.utils.AppContextUtil;
import com.tianyang.yukuaifu.utils.SystemBarHelper;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：我的服务商
 * Created by xyj on 2024/3/7
 */
public class MyServiceFragment extends BaseFragment {

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.statusTv)
    TextView statusTv;
    @BindView(R.id.inviteCodeTv)
    TextView inviteCodeTv;

    private int verifyStatus;

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_my_service;
    }

    @Override
    protected void initView() {
        super.initView();
        int barHeight = SystemBarHelper.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) topView.getLayoutParams();
        params.height = barHeight;
        topView.setLayoutParams(params);
    }

    @OnClick({R.id.authRl, R.id.inviteCodeRl, R.id.bankRl, R.id.rateRl, R.id.updatePwdRl, R.id.exitRl})
    public void onClick(View view) {
        if (!isRespondEvent() || getContext() == null) return;
        switch (view.getId()) {
            case R.id.authRl:
                if (verifyStatus >= 1) {
                    ToastUtil.show("已完成实名");
                    return;
                }
                AuthOpenProvider.getInstance().openAuthServiceActivity(getContext());
                break;
            case R.id.inviteCodeRl:
//                if (!checkVerifyStatus()) return;
//                MainOpenProvider.getInstance().openInviteCodeActivity(getContext());
                break;
            case R.id.bankRl:
//                if (!checkVerifyStatus()) return;
                TradeOpenProvider.getInstance().openBankManageActivity(getContext(), true);
                break;
            case R.id.rateRl:
                UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
                if (output != null) {
                    MainOpenProvider.getInstance().openRateConfigActivity(
                            getContext(), output.getUsername(), 1, false);
                }
                break;
            case R.id.updatePwdRl:
                LoginOpenProvider.getInstance().openUpdateLoginPwdActivity(getContext());
                break;
            case R.id.exitRl:
                new RemindDialog(getContext())
                        .title("退出登录")
                        .content("是否退出当前账号？")
                        .listener(new TwoBtnClickListener() {
                            @Override
                            public void onLeftClick() {

                            }

                            @Override
                            public void onRightClick() {
                                AppContextUtil.exit(getContext());
                            }
                        })
                        .show();
                break;
        }
    }

    private boolean checkVerifyStatus() {
        if (verifyStatus < 1) {
            new HintDialog(getContext())
                    .title("温馨提示")
                    .content("请先完成实名认证")
                    .showBtn("去认证")
                    .listener(new OnBtnClickL() {
                        @Override
                        public void onBtnClick() {
                            AuthOpenProvider.getInstance().openAuthServiceActivity(getContext());
                        }
                    })
                    .show();
            return false;
        }
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        UserCenterOutput mOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (mOutput != null) {
            showViewByData(mOutput);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveEntity(UserCenterOutput event) {
        if (event != null) {
            showViewByData(event);
        }
    }

    private void showViewByData(UserCenterOutput output) {
        if (output == null) return;
        verifyStatus = output.getVerifyStatus();
        statusTv.setText(verifyStatus >= 1 ? "已实名" : "未实名");
        titleTv.setText(verifyStatus >= 1 ? VerifyUtil.checkString(output.getAgentName()) : VerifyUtil.checkString(output.getUsername()));
        inviteCodeTv.setText(VerifyUtil.checkString(output.getInvitecode()));
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }
}
